﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HM.CS;

namespace HM
{
    public partial class ServerSet : Form
    {
        public ServerSet()
        {
            InitializeComponent();
            GetConfigInfo();
            SetFormWithAndHeith(this);
        }


        

        private void btnOK_Click(object sender, EventArgs e)
        {
            string sIP;
            string sPort;
            string sRepeat;
            CXmlClass xml = new CXmlClass();

            sIP = txtServerIP.Text.Trim();
            sPort = txtServerPort.Text.Trim();
            sRepeat = "1";
            if (!ckbYes.Checked)
            {
                sRepeat = "0";
            }
            xml.WriteXML(sIP, sPort, sRepeat);
            MessageBox.Show("保存成功");
            this.Hide();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void GetConfigInfo()
        {
            CXmlClass xml = new CXmlClass();
            string sip;
            string sport;
            sip = "";
            sport = "";
            string sRepeat;
            sRepeat = "0";
            DataTable dt = xml.ReadXML();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sip = dr["IP"].ToString();
                    sport = dr["Port"].ToString();
                    sRepeat = dr["Repeat"].ToString();
                }
            }

            txtServerIP.Text = sip;
            txtServerPort.Text = sport;
            if (sRepeat == "1")
            {
                ckbYes.Checked = true;
            }
            else
            {
                ckbYes.Checked = false;
            }
        }

        private void SetFormWithAndHeith(Form obj)
        {
            Rectangle ScreenArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            int width = ScreenArea.Width;
            int height = ScreenArea.Height;
            obj.Width = width - 5;
            obj.Height = height - 5;
        }
    }
}